package com.Interface.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.Interface.util.MatchUtil;

public class StringUtil {
	
	public static String toString(String txt) {
		return StringUtil.toString(txt, "");
	}
	
	public static String toString(Object obj) {
		return StringUtil.toString(obj, "");
	}
	
	public static String toString(Throwable obj) {
		Throwable src = obj.getCause();
		try {
			return MatchUtil.isEmpty(src) ? StringUtil.toString(
				obj.getMessage(), obj.toString()
			) : StringUtil.toString(
				src.getMessage(), src.toString()
			);
		} finally {
			if (MatchUtil.isEmpty(src)) {
				obj.printStackTrace();
			} else {
				src.printStackTrace();
			}
		}
	}
	
	public static String toString(String txt, Object str) {
		return StringUtil.toString(txt, str + "");
	}
	
	public static String toString(String txt, String str) {
		return MatchUtil.isEmpty(txt) ? str : txt;
	}
	
	public static String toString(Object obj, Object str) {
		return StringUtil.toString(obj, str + "");
	}
	
	public static String toString(Object obj, String str) {
		return MatchUtil.isEmpty(obj) ? str: obj + "";
	}
	
	/**
	 * 全角转换半角
	 * @param input
	 * @return
	 */
	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i< c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i]> 65280&& c[i]< 65375) c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}
	
	public static boolean isNull(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isNotNull(String str) {
		return !isNull(str);
	}
	
	/**
	 * 字符串验证正则 
	 * @param s  要验证的串
	 * @return
	 */
	public static boolean isNumeric(String str){ 
	    Pattern pattern = Pattern.compile("[0-9]*"); 
	    return pattern.matcher(str).matches();    
	 } 
	
	/**
	 * 去掉字符串最后的一位逗号
	 * @param str
	 * @return
	 */
	public static String delDot(String str){
		if(str.endsWith(","))
			str =  str.substring(0,str.length()-1);
		return str;
	}
	
	public static String createOracleIn(String str){
		String [] tmp = str.split(",");
		String t = "";
		for(int i =0;i<tmp.length;i++){
			t += "'"+tmp[i]+"',";
		}
		t = delDot(t);
		return t;
	}
	
	/**
	 * 验证日期模式 yyyy-mm-dd
	 */
    public static boolean checkDate(String date){
    	String eL = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
		Pattern p = Pattern.compile(eL);
		Matcher m = p.matcher(date);
		boolean dateFlag = m.matches();
		if (!dateFlag) {
			//System.out.println("格式错误");
			return false;
		}
		//System.out.println("格式正确");
		return true;
    }
    
    public static String toUtf8String(String s){
    	StringBuffer sb=new StringBuffer();
    	for(int i=0;i<s.length();i++){
    		char c=s.charAt(i);
    		if(c>=0&&c<=255){
    			sb.append(c);
    		}else{
    			byte[]b;
    			try {
					b=Character.toString(c).getBytes("utf-8");
				} catch (Exception e) {
					b=new byte[0];
				}
				for(int j=0;j<b.length;j++){
					int k=b[j];
					if(k<0){
						k+=256;
						sb.append("%"+Integer.toHexString(k).toUpperCase());
					}
				}
    		}
    	}
    	
    	return sb.toString();
    }
    
    /**
     * 
     * @param a 需要转换的值
     * @param b 转换的数据类型
     * @return 是否转换成功
     */
    public static boolean dataType(String a,String b){
    	if("".equals(a.trim())||a==null){
    		return true;
    	}
    	if(b.equals("Integer")){
    		try {
    			Integer.valueOf(a);
			} catch (Exception e) {
//				System.out.println("e:"+e);
				return false;
			}
    	}
    	if(b.equals("string")){
    		try {
    			String.valueOf(a);
			} catch (Exception e) {
//				System.out.println("e:"+e);
				return false;
			}
    	}
    	if(b.equals("int")){
    		try {
    			Integer.valueOf(a);
			} catch (Exception e) {
//				System.out.println("e:"+e);
				return false;
			}
    	}
    	if(b.equals("float")){
    		try {
    			Float.valueOf(a);
			} catch (Exception e) {
//				System.out.println("e:"+e);
				return false;
			}
    	}
    	if(b.equals("long")){
    		try {
    			Long.valueOf(a);
			} catch (Exception e) {
//				System.out.println("e:"+e);
				return false;
			}
    	}
    	if(b.equals("double")){
    		try {
    			Double.valueOf(a);
			} catch (Exception e) {
//				System.out.println("e:"+e);
				return false;
			}
    	}
		return true;
    }
    
    /**
	 * 取得配置文件内容
	 * @param propfilename 配置文件路径及文件名 如config/locate.properties
	 * @param propname 属性名
	 * @return
	 */
	public static String getPropUrl(String propfilename,String propname) {
		//"config/locate.properties"
		Properties  props = new Properties();  
	     try {
	    	 
			props.load(StringUtil.class.getClassLoader().getResourceAsStream(propfilename));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		return props.getProperty(propname);
	}
	
	public static String getSpeedingWebservice(){
		return StringUtil.getPropUrl("http.properties", "speeding.webservice");
	}
	
	public static String getTrackSiteservice(){
		return StringUtil.getPropUrl("http.properties", "trackSiteService");
	}
	
	public static String getPoiService(){
		return StringUtil.getPropUrl("http.properties", "poiService");
	}
	
	public static String getHost(){
		return StringUtil.getPropUrl("http.properties", "host");
	}
	public static String getPort(){
		return StringUtil.getPropUrl("http.properties", "port");
	}
}